Welcome to “Building Container
Images for Microservices”. After watching this video, you will be able to explain what a Container is, explain what a Docker is, and build a Docker container image. A container is a standalone, all-inclusive,
and executable unit of software. The application's source code is packaged
within the container with its libraries, dependencies, and runtimes. Once built, the container can run on any device, such as a laptop, desktop
PC, or on-premises servers. It can also run in the cloud. Now containers are small, fast, and portable because, unlike a virtual machine, they do not require​
a guest operating system with every instance, and instead can leverage the features ​and
resources of the host operating system. One of the key elements of a microservice
is that it is generally small and isolated in terms of computational
resources for each running instance. This makes the container a perfect match for such small and lightweight services
within microservices architectures. Docker is a software platform for building
and running applications as containers Docker leads to an explosive
usage of containerization. In addition, a huge collection of complementary
tools, technologies, and development methodologies are still growing and building up a big
Docker and containerization economy. Docker provides a straightforward way to build and
run containers through an open-source platform. Docker is the main container
technology used in IBM Cloud Engine. A container image is the basis
of a cloud engine application. Let’s now discuss a typical way to build a
container from source code using a Dockerfile. Suppose you have completed the application
development and tested it locally in an IDE with its source code and
dependencies, and libraries. And now, you want to build a docker container. The first step is to create a Dockerfile. A Dockerfile acts like a blueprint
from which an image is built. It outlines all the instructions
to build the desired container. A container image is built
after the Dockerfile is created. Note that a container image and a
container are two distinct things. An image is an immutable file containing all the
application assets like source code, libraries, and dependencies necessary
for an application to run. And the images are read-only; if you change
an image, you create a new one. Like object-oriented programming, consider a container image like a class that
acts like a template of a running container. Therefore, if we instantiate a container image,
we get a running image called the container. So basically, you only need to write a proper
Dockerfile, and the Docker handles the rest. Next, let’s see a Dockerfile example
for building a Flask-based microservice. The first line of a Dockerfile starts
with the FROM instruction to specify the base image that subsequent
instructions will build upon. This base image is often
from a public repository, like an operating system, or a base image
from a specific language like Python here. Optionally, instead of the
general Python docker image, you may add a more specific and advanced
base image to better serve your microservice. For example, the uwsgi-nginx-flask
allows you to create the Flask microservice in Python that runs with
uWSGI and Nginx in a single container. The next Copy instruction copies all the
files to the /app folder in the image. You can use the ENV instruction
to set the environment variable. Here you set the LISTEN PORT to
be 8080 meaning the container made from this image will listen on port 8080. The expose keyword in a Dockerfile indicates to the Docker that a container
listens for traffic on the 8080. And the RUN instruction executes commands. For example, here we run pip install to
install all required python libraries. And the last one is the CMD. The main purpose for this instruction is to
provide a default for executing a container. This instruction often defines the executable
that should run in your container. In the example, you run the flask microservice. But note that there can only be one
CMD instruction in a Dockerfile. After your container image is built, you can push
it to a container repository to be better managed. This container repository is
called a container registry. Some common registries are Docker
Hub and IBM Cloud Container Registry. The container image in the registry can be
easily pulled and used with a unique image name. An image name consists of a
hostname, repository, and a tag. The hostname identifies the registry
to which the image should be pushed. For example, the icr.io represents
the IBM Container Registry. Now a repository is a group of
related container images. And usually, these will be different versions
of the same application or service, so the name of the application or
service makes a good repository name. Finally, the tag provides information about
an image's specific version or variant. In this video, you learned that: A container is a standalone executable software
unit packaged with all its dependencies. Docker is a very popular
container-building and running platform. You can compose a Dockerfile to instruct the
Docker platform to build a container image. And after the container image is built, it can be pushed to the container registry
and then pulled by its image name.